<template>
	<view>
		<view class="goods-list">
			<view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
				<my-goods :item="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj:{
					// 查询关键词
					query:'',
					// 商品分类id
					cid:'',
					// 页码值
					pagenum:1,
					// 每页显式多少个数据
					pagesize:10
				},
				// 商品列表
				goodsList:[],
				total:0,
				// 节流阀
				isLoading:false
			};
		},
		methods:{
			async getGoodsList(cb){
				
				// 打开节流阀
				this.isLoading =true
		
				const res = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				
				// 关闭节流阀
				this.isLoading = false
				
				cb && cb();
				if(res.data.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList,...res.data.message.goods]
				this.total = res.data.message.total
			},
			gotoDetail(item){
				uni.navigateTo({
					url:"/subpkg/goods-detail/goods-detail?goods_id="+item.goods_id
				})
			}
		},
		onReachBottom(){
			// 当页码值*每页长度大于总的数据时 说明下一页没数据了，那就return提示数据加载完毕并且不请求数据了
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg("数据加载完毕！")
			if(this.isLoading) return 
			this.queryObj.pagenum ++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.total = 0 
			this.isLoading = false
			this.goodsList = []
			
			this.getGoodsList(()=>uni.stopPullDownRefresh())
			
		},
		onLoad(options) {
			
			this.queryObj.query = options.query || '',
			this.queryObj.cid = options.cat_id || ''
			this.getGoodsList()
			
		},
	
	}
</script>  

<style lang="scss">

</style>
